-------------------------------------------------------------------------------
-- Functions & Variables
-------------------------------------------------------------------------------
if not PQR_LoadedDataFile then
	PQR_LoadedDateFile = 1
	print("|cffFFBE69Deva Data File v1.0 - 05/12/2012|cffffffff")
elseif PQR_LoadedDataFile then
	print("|cffFFBE69Reloading: Deva Data File v1.0|cffffffff")
end

PQ_UnitInfo = nil
function PQ_UnitInfo(t)
	--Takes an input of UnitID (player, target, pet, mouseover, etc) and gives you their most useful info
	local TManaActual = UnitPower(t)
	local TMana = 100 * UnitPower(t) / UnitPowerMax(t)
	local THealthActual = UnitHealth(t)
	local THealth = 100 * UnitHealth(t) / UnitHealthMax(t)
	local myClassPower = 0
	local PQ_Class = select(2, UnitClass(t))
	local PQ_UnitLevel = UnitLevel(t)
	local PQ_CombatCheck = UnitAffectingCombat(t)
	if PQ_Class == "PALADIN" then
		myClassPower = UnitPower("player", 9)
	elseif PQ_Class == "WARLOCK" then
		myClassPower = UnitPower("player", 7)
	elseif PQ_Class == "DRUID" and GetPrimaryTalentTree() == 2 then
		myClassPower = UnitPower("player", 8)
	elseif PQ_Class == "MONK"  then
		myClassPower = UnitPower("player", 12)
	end
	--     1        2              3      4            5             6         7             8
	return THealth, THealthActual, TMana, TManaActual, myClassPower, PQ_Class, PQ_UnitLevel, PQ_CombatCheck
end

if GetSpecialization then
	PQR_SavedSpec = GetSpecialization()
else
	PQR_SavedSpec = GetPrimaryTalentTree()
end
PQR_LevelInfo = UnitLevel("player")


PQ_CastingInfo = nil
function PQ_CastingInfo(PQ_Unit)
	local PQ_Casting = nil
	local percentRemaining = nil
	local secondsRemaining = nil

	if UnitCastingInfo(PQ_Unit) then
		local spellName,_,_,_,startTimer,endTimer = UnitCastingInfo(PQ_Unit)
		local durationTimer = ((endTimer) - (startTimer))
		local timeRemaining = (endTimer - GetTime() * 1000)
		percentRemaining = (100 - ((timeRemaining/durationTimer) * 100))
		secondsRemaining = endTimer/1000 - GetTime()
		PQ_Casting = spellName
		-- Name of spell, % remaining on spell, seconds remaining on spell
		return PQ_Casting, percentRemaining, secondsRemaining
	else return false end
end

-------------------------------------------------------------------------------
-- Debug & Notification Frame
-------------------------------------------------------------------------------
-- Update Debug Frame
PQ_NotifyFrame = nil
function PQ_NotifyFrame_OnUpdate()
	if (PQ_NotifyFrameTime < GetTime() - 5) then
		local alpha = PQ_NotifyFrame:GetAlpha()
		if (alpha ~= 0) then PQ_NotifyFrame:SetAlpha(alpha - .02) end
		if (aplha == 0) then PQ_NotifyFrame:Hide() end
	end
end

-- Debug messages.
function PQ_Notify(message)
	PQ_NotifyFrame.text:SetText(message)
	PQ_NotifyFrame:SetAlpha(1)
	PQ_NotifyFrame:Show()
	PQ_NotifyFrameTime = GetTime()
end

-- Debug Notification Frame
PQ_NotifyFrame = CreateFrame('Frame')
PQ_NotifyFrame:ClearAllPoints()
PQ_NotifyFrame:SetHeight(300)
PQ_NotifyFrame:SetWidth(300)
PQ_NotifyFrame:SetScript('OnUpdate', PQ_NotifyFrame_OnUpdate)
PQ_NotifyFrame:Hide()
PQ_NotifyFrame.text = PQ_NotifyFrame:CreateFontString(nil, 'BACKGROUND', 'PVPInfoTextFont')
PQ_NotifyFrame.text:SetAllPoints()
PQ_NotifyFrame:SetPoint('CENTER', 0, 200)
PQ_NotifyFrameTime = 0

-------------------------------------------------------------------------------
-- Target & Environmental Globals and Tables
-------------------------------------------------------------------------------
PQ_Immunity = {
	642,45438,31224,23920,33786,19263,97417,97977,105256,105409,
	105465,108568,109552,109553,109554,109560,109561,109562,110892,110893
}
PQ_FL = {110070, 105925, 109075, 110079, 110069, 110070, 110080, 110068, 110078}
PQ_SN = {110139,110140,110141,106791,109599,106794,109597,109598}
PQ_HOT = {109417,109416,109415,106371}


-------------------------------------------------------------------------------
-- Buff Globals
-------------------------------------------------------------------------------
-- Temporary Buffs
PQ_Lightweave		= 75170
PQ_PowerTorrent		= 74241
PQ_VolcanicPotion	= 79476
PQ_SynapseSprings	= 96230
PQ_BloodLust		= 2825
PQ_Heroism			= 32182
PQ_TimeWarp			= 80353
PQ_Hysteria			= 90355
PQ_Zerk				= 26297

-- Temporary Buff Table
PQ_TemporaryBuffs = {
	{spellID = PQ_Lightweave, check = true, castIt = true, hasBuff = false, endTime = nil},
	{spellID = PQ_PowerTorrent, check = true, castIt = true, hasBuff = false, endTime = nil},
	{spellID = PQ_VolcanicPotion, check = true, castIt = true, hasBuff = false, endTime = nil},
	{spellID = PQ_SynapseSprings, check = true, castIt = true, hasBuff = false, endTime = nil}
}

-------------------------------------------------------------------------------
-- Druid Globals
-------------------------------------------------------------------------------	
if select(2, UnitClass("player")) == "DRUID" then
	print("|cffFFBE69Loading |cffC27C0CDruid|cffFFBE69 Tables ...|cffffffff")
	-- Constants
	PQ_Barkskin = 22812
	PQ_HealingTouch = 5185
	PQ_Innervate = 29166
	PQ_Lifebloom = 33763
	PQ_MotW = 1126
	PQ_NaturesGrasp = 16689
	PQ_Nourish = 50464
	PQ_Regrowth = 8936
	PQ_Rejuvenation = 774
	PQ_RemoveCorruption = 2782
	PQ_Swiftmend = 18562
	PQ_Tranquility = 740
	PQ_TreeOfLife = 33891
	PQ_WildGrowth = 48438

	PQ_AquaticForm = 1066
	PQ_TravelForm = 783
	PQ_Dash = 1850
	PQ_FaerieFireFeral = 16857

	-- Feral (PQC for Cat, PQB for Bear)
	PQ_Berserk = 50334
	PQ_SurvivalInstincts = 61336

	PQC_CatForm = 768
	PQB_BearForm = 5487
	
	PQB_Bash = 5211
	PQB_ChallengingRoar = 5209
	PQB_DemoralizingRoar = 99
	PQB_Enrage = 5229
	PQB_FeralCharge = 16979
	PQB_FrenziedRegeneration = 22842
	PQB_Growl = 6795
	PQB_Lacerate = 33745
	PQB_Mangle = 33878
	PQB_Maul = 6807
	PQB_Pulverize = 80313
	PQB_SkullBash = 80964
	PQB_StampedingRoar = 77761
	PQB_Swipe = 779
	PQB_Thrash = 77758
	
	PQC_Claw = 1082
	PQC_Cower = 8998
	PQC_FeralCharge = 49376
	PQC_FerociousBite = 22568
	PQC_Maim = 22570
	PQC_Mangle = 33876
	PQC_Pounce = 9005
	PQC_Prowl = 5215
	PQC_Rake = 1822
	PQC_Ravage = 6785
	PQC_RavageBang = 81170
	PQC_Rip = 1079
	PQC_SavageRoar = 52610
	PQC_Shred = 5221
	PQC_SkullBash = 80965
	PQC_StampedingRoar = 77764
	PQC_Swipe = 62078
	PQC_TigersFury = 5217
	

	-- The Table
	PQ_Spell = {
		-- Resto and General
		[PQ_Barkskin] 	=	 { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(22812), playerHealth = 65 },
		[PQ_HealingTouch]		=	 { check = true, castIt = true, castTime = 2.5, isKnown = IsSpellKnown(5185), targetHealth = 48, overHealLimit = 112, overRide = false },
		[PQ_Innervate] 		=	 { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(29166), playerMana = 65 },
		[PQ_Lifebloom] 		=	 { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(33763), targetName = "" },
		[PQ_MotW] 				=	 { check = true, castIt = true, isKnown= IsSpellKnown(1126) },
		[PQ_NaturesGrasp] = { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(16689) },
		[PQ_Nourish] 			=	 { check = true, castIt = true, castTime = 2.5, isKnown = IsSpellKnown(50464), targetHealth = 75, overHealLimit = 107, overRide = false },
		[PQ_Regrowth]		=	 { check = true, castIt = true, castTime = 1.5, isKnown = IsSpellKnown(8936), targetHealth = 28, overHealLimit = 102, overRide = false },
		[PQ_Rejuvenation] 		=	 { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(774), targetHealth = 80, overRide = false },
		[PQ_RemoveCorruption] 			=	 { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(2782), prioDispel = 0 },
		[PQ_Swiftmend] 		=	 { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(18562), targetNumber = 3, targetHealth = 90, secondarytargetHealth = 90, range = 7, overRide = false },
		[PQ_Tranquility] = { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(740) },
		[PQ_TreeOfLife] = { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(33891) },
		[PQ_WildGrowth] 		=	 { check = true, castIt = true, castTime = 0, isKnown = IsSpellKnown(48438), targetNumber = 3, targetHealth = 90, range = 40, overRide = false },

		-- Feral 
		[PQC_CatForm] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_CatForm), playerHealth = 65 },
		[PQB_BearForm] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_BearForm), playerHealth = 40 },

		-- Cat Abilities
		[PQC_Claw] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Claw), minLevel = 8, maxLevel = 9, cost = 40 },
		[PQC_Cower] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Claw), minLevel = 26, cost = 20 },
		[PQC_FeralCharge] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_FeralCharge), minLevel = 20, cost = 10 },
		[PQC_FerociousBite] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_FerociousBite), minLevel = 8, cost = 25, minCP = 1, maxCP = 5 },
		[PQC_Maim] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Maim), minLevel = 62, cost = 35, minCP = 1, maxCP = 5 },
		[PQC_Mangle] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Mangle), minLevel = 10, cost = 35 },
		[PQC_Pounce] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Pounce), minLevel = 36, cost = 50 },
		[PQC_Prowl] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Prowl), minLevel = 10 },
		[PQC_Rake] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Rake), minLevel = 8, cost = 35 },
		[PQC_Ravage] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Ravage), minLevel = 22, cost = 60 },
		[PQC_RavageBang] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_RavageBang), minLevel = 22, cost = 60 },
		[PQC_Rip] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Rip), minLevel = 54, cost = 30, minCP = 1, maxCP = 5 },
		[PQC_SavageRoar] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_SavageRoar), minLevel = 76, cost = 25, minCP = 1, maxCP = 5 },
		[PQC_Shred] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Shred), minLevel = 8, cost = 40 },
		[PQC_SkullBash] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_SkullBash), minLevel = 22, cost = 25 },
		[PQC_StampedingRoar] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_StampedingRoar), minLevel = 83, cost = 30 },
		[PQC_Swipe] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_Swipe), minLevel = 36, cost = 45 },
		[PQC_TigersFury] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQC_TigersFury), minLevel = 24 },

		-- Bear Abilities
		[PQB_Bash] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Bash), minLevel = 32, cost = 10 },
		[PQB_ChallengingRoar] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_ChallengingRoar), minLevel = 28, cost = 15 },
		[PQB_DemoralizingRoar] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_DemoralizingRoar), minLevel = 15, cost = 10 },
		[PQB_Enrage] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Enrage), minLevel = 22 },
		[PQB_FeralCharge] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_FeralCharge), minLevel = 20, cost = 5 },
		[PQB_FrenziedRegeneration] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_FrenziedRegeneration), minLevel = 52 },
		[PQB_Growl] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Growl), minLevel = 15 },
		[PQB_Lacerate] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Lacerate), minLevel = 66, cost = 15 },
		[PQB_Mangle] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Mangle), minLevel = 10, cost = 15 },
		[PQB_Maul] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Maul), minLevel = 15, cost = 30 },
		[PQB_Pulverize] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Pulverize), minLevel = 59, cost = 15 },
		[PQB_SkullBash] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_SkullBash), minLevel = 22, cost = 15 },
		[PQB_StampedingRoar] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_StampedingRoar), minLevel = 83, cost = 15 },
		[PQB_Swipe] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Swipe), minLevel = 18, cost = 15 },
		[PQB_Thrash] = { castIt = true, castTime = 0, isKnown = IsSpellKnown(PQB_Thrash), minLevel = 81, cost = 25 }
	}
end

-- sheuron's functions (Swiftmend testing)
function CheckDistance(unit1,unit2) 
  local x1,y1 = GetPlayerMapPosition(unit1)
  local x2,y2 = GetPlayerMapPosition(unit2)
  return sqrt( (x1-x2)^2 + (y1-y2)^2 )
end
  
function UnitsClose(t)
  local n = 0
  if distance and distance[1] then
    for i=1,#members do
      local x = CheckDistance(t,members[i].Unit)
      if x ~= 0 and x < distance[1] and members[i].HP < 95 then n = n + 1 end
    end
  end
  return n
end

function GetDistance() 
  local playerx,playery = GetPlayerMapPosition("player") 
  if GetCurrentMapAreaID() ~= xrnMap or GetCurrentMapDungeonLevel() ~= xrnDung then 
    xrnMap,xrnDung = GetCurrentMapAreaID(), GetCurrentMapDungeonLevel() 
    mp, distance = {}, {} 
  end
  if #members > 1 and #distance < 10 and playerx ~= 0 and playery ~= 0 then
    for i=1,#members do
      if CheckInteractDistance(members[i].Unit,2) then 
        mp[UnitGUID(members[i].Unit)] = {GetPlayerMapPosition(members[i].Unit)} 
      elseif UnitInRange(members[i].Unit) and mp[UnitGUID(members[i].Unit)] then
        table.insert(distance,sqrt((mp[UnitGUID(members[i].Unit)][1] - playerx)^2 + (mp[UnitGUID(members[i].Unit)][2] - playery)^2)) 
        table.sort(distance) 
        mp[UnitGUID(members[i].Unit)] = nil
      end
    end
  end
end

-- My Swiftmend function, returns true/false, example: 
-- if CanSwiftmend(PQR_CustomTarget) then return true end

function CanSwiftmend(unit)
	local HoTs = {
	774, -- Rejuvenation
	8936 -- Regrowth
	}
	for i=1,#HoTs do
		if UnitBuffID(unit,HoTs[i]) then return true end
	end
	return false
end